home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / BACKFACE.FRM < prev    next >
Text File  |  1996-05-02  |  9KB  |  358 lines

  1. VERSION 4.00
  2. Begin VB.Form BackfaceForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Backface"
  6.    ClientHeight    =   5685
  7.    ClientLeft      =   1410
  8.    ClientTop       =   825
  9.    ClientWidth     =   6015
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6375
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1350
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5685
  25.    ScaleWidth      =   6015
  26.    Top             =   195
  27.    Width           =   6135
  28.    Begin VB.CheckBox CullCheck 
  29.       Caption         =   "Cull"
  30.       Height          =   255
  31.       Left            =   5160
  32.       TabIndex        =   7
  33.       Top             =   5400
  34.       Width           =   735
  35.    End
  36.    Begin VB.TextBox PhiText 
  37.       Height          =   285
  38.       Left            =   3600
  39.       TabIndex        =   6
  40.       Text            =   "0.1570"
  41.       Top             =   5400
  42.       Width           =   855
  43.    End
  44.    Begin VB.TextBox ThetaText 
  45.       Height          =   285
  46.       Left            =   2040
  47.       TabIndex        =   4
  48.       Text            =   "0.6283"
  49.       Top             =   5400
  50.       Width           =   855
  51.    End
  52.    Begin VB.TextBox RText 
  53.       Height          =   285
  54.       Left            =   480
  55.       TabIndex        =   2
  56.       Text            =   "10"
  57.       Top             =   5400
  58.       Width           =   855
  59.    End
  60.    Begin VB.PictureBox Pict 
  61.       AutoRedraw      =   -1  'True
  62.       Height          =   5295
  63.       Left            =   0
  64.       ScaleHeight     =   -14
  65.       ScaleLeft       =   -7
  66.       ScaleMode       =   0  'User
  67.       ScaleTop        =   7
  68.       ScaleWidth      =   15.926
  69.       TabIndex        =   0
  70.       Top             =   0
  71.       Width           =   6015
  72.    End
  73.    Begin VB.Label Label1 
  74.       Caption         =   "Phi"
  75.       Height          =   255
  76.       Index           =   2
  77.       Left            =   3240
  78.       TabIndex        =   5
  79.       Top             =   5400
  80.       Width           =   375
  81.    End
  82.    Begin VB.Label Label1 
  83.       Caption         =   "Theta"
  84.       Height          =   255
  85.       Index           =   1
  86.       Left            =   1440
  87.       TabIndex        =   3
  88.       Top             =   5400
  89.       Width           =   495
  90.    End
  91.    Begin VB.Label Label1 
  92.       Caption         =   "R"
  93.       Height          =   255
  94.       Index           =   0
  95.       Left            =   240
  96.       TabIndex        =   1
  97.       Top             =   5400
  98.       Width           =   255
  99.    End
  100.    Begin MSComDlg.CommonDialog LoadDialog 
  101.       Left            =   3000
  102.       Top             =   5280
  103.       _version        =   65536
  104.       _extentx        =   847
  105.       _extenty        =   847
  106.       _stockprops     =   0
  107.       cancelerror     =   -1  'True
  108.    End
  109.    Begin VB.Menu mnuFile 
  110.       Caption         =   "&File"
  111.       Begin VB.Menu mnuFileLoad 
  112.          Caption         =   "&Load..."
  113.          Shortcut        =   ^L
  114.       End
  115.       Begin VB.Menu mnuFileSep 
  116.          Caption         =   "-"
  117.       End
  118.       Begin VB.Menu mnuFileExit 
  119.          Caption         =   "E&xit"
  120.       End
  121.    End
  122. End
  123. Attribute VB_Name = "BackfaceForm"
  124. Attribute VB_Creatable = False
  125. Attribute VB_Exposed = False
  126. Option Explicit
  127.  
  128. ' Location of viewing eye.
  129. Dim EyeR As Single
  130. Dim EyeTheta As Single
  131. Dim EyePhi As Single
  132.  
  133. Const Dtheta = PI / 20
  134. Const Dphi = PI / 20
  135. Const Dr = 1
  136.  
  137. ' Location of focus point.
  138. Const FocusX = 0#
  139. Const FocusY = 0#
  140. Const FocusZ = 0#
  141.  
  142. Dim Projector(1 To 4, 1 To 4) As Single
  143.  
  144. Dim ThePicture As ObjPicture
  145.  
  146. Dim ShowingParameters As Boolean
  147. ' *******************************************************
  148. ' Rotate the points in the cube and draw the cube.
  149. ' *******************************************************
  150. Private Sub DrawData(pic As Object)
  151. Dim X As Single
  152. Dim Y As Single
  153. Dim z As Single
  154.  
  155.     ' Prevent overflow errors when drawing lines
  156.     ' too far out of bounds.
  157.     On Error Resume Next
  158.     
  159.     ' Cull backfaces.
  160.     ThePicture.Culled = False
  161.     If CullCheck.value = vbChecked Then
  162.         m3SphericalToCartesian EyeR, EyeTheta, EyePhi, X, Y, z
  163.         ThePicture.Cull X, Y, z
  164.     End If
  165.     
  166.     ' Transform the points.
  167.     ThePicture.ApplyFull Projector
  168.  
  169.     ' Display the data.
  170.     pic.Cls
  171.     ThePicture.Draw pic, EyeR
  172.     pic.Refresh
  173.  
  174.     ' Display the viewing parameters.
  175.     ShowViewingParameters
  176. End Sub
  177.  
  178. Sub ShowViewingParameters()
  179.     ShowingParameters = True
  180.     
  181.     RText.Text = Format$(EyeR, "0.0000")
  182.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  183.     PhiText.Text = Format$(EyePhi, "0.0000")
  184.     
  185.     RText.Refresh
  186.     ThetaText.Refresh
  187.     PhiText.Refresh
  188.     
  189.     ShowingParameters = False
  190. End Sub
  191.  
  192.  
  193. ' ************************************************
  194. ' Redraw the picture with culling changed.
  195. ' ************************************************
  196. Private Sub CullCheck_Click()
  197.     DrawData Pict
  198.     Pict.SetFocus
  199. End Sub
  200.  
  201. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  202.     Select Case KeyCode
  203.         Case vbKeyLeft
  204.             EyeTheta = EyeTheta - Dtheta
  205.         
  206.         Case vbKeyRight
  207.             EyeTheta = EyeTheta + Dtheta
  208.         
  209.         Case vbKeyUp
  210.             EyePhi = EyePhi - Dphi
  211.         
  212.         Case vbKeyDown
  213.             EyePhi = EyePhi + Dphi
  214.                 
  215.         Case Else
  216.             Exit Sub
  217.     End Select
  218.  
  219.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  220.     DrawData Pict
  221. End Sub
  222.  
  223.  
  224. Private Sub Form_KeyPress(KeyAscii As Integer)
  225.     Select Case KeyAscii
  226.         Case Asc("+")
  227.             EyeR = EyeR + Dr
  228.         
  229.         Case Asc("-")
  230.             EyeR = EyeR - Dr
  231.         
  232.         Case Else
  233.             Exit Sub
  234.     End Select
  235.  
  236.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  237.     DrawData Pict
  238. End Sub
  239.  
  240. Private Sub Form_Load()
  241.     ' Initialize the eye position.
  242.     EyeR = 10
  243.     EyeTheta = PI * 0.2
  244.     EyePhi = PI * 0.05
  245.     
  246.     ' Initialize the projection transformation.
  247.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  248.     
  249.     ' Create the data.
  250.     CreateData
  251.  
  252.     ' Project and draw the data.
  253.     DrawData Pict
  254. End Sub
  255.  
  256. Sub CreateData()
  257. Dim pline As ObjPolyline
  258.     
  259.     Set ThePicture = New ObjPicture
  260.     Set pline = New ObjPolyline
  261.     ThePicture.Objects.Add pline
  262.     
  263.     pline.AddSegment 0, 0, 0, 5, 0, 0
  264.     pline.AddSegment 0, 0, 0, 0, 5, 0
  265.     pline.AddSegment 0, 0, 0, 0, 0, 5
  266. End Sub
  267.  
  268. Private Sub mnuFileExit_Click()
  269.     Unload Me
  270. End Sub
  271.  
  272.  
  273. Private Sub mnuFileLoad_Click()
  274. Dim fname As String
  275. Dim filenum As Integer
  276. Dim txt As String
  277. Dim xmin As Single
  278. Dim ymin As Single
  279. Dim xmax As Single
  280. Dim ymax As Single
  281.  
  282.     ' Allow the user to pick a file.
  283.     On Error Resume Next
  284.     LoadDialog.filename = "*.APF"
  285.     LoadDialog.ShowOpen
  286.     If Err.Number = cdlCancel Then
  287.         Unload LoadDialog
  288.         Exit Sub
  289.     ElseIf Err.Number <> 0 Then
  290.         Unload LoadDialog
  291.         Beep
  292.         MsgBox "Error selecting file.", , vbExclamation
  293.         Exit Sub
  294.     End If
  295.     On Error GoTo 0
  296.     
  297.     fname = LoadDialog.filename
  298.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  299.         - Len(LoadDialog.FileTitle) - 1)
  300.  
  301.     ' Clear the picture.
  302.     Set ThePicture = Nothing
  303.     
  304.     ' Open the file.
  305.     filenum = FreeFile
  306.     Open fname For Input As #filenum
  307.     
  308.     ' Make sure it's an Object Picture File.
  309.     Input #filenum, txt
  310.     If txt <> "3D APF PICTURE" Then
  311.         Close filenum
  312.         Beep
  313.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  314.         Exit Sub
  315.     End If
  316.  
  317.     ' Read the picture.
  318.     Set ThePicture = New ObjPicture
  319.     ThePicture.FileInput filenum
  320.     
  321.     ' Close the file.
  322.     Close filenum
  323.  
  324.     ' Refresh the display.
  325.     DrawData Pict
  326.     
  327.     Caption = "Backface [" & LoadDialog.FileTitle & "]"
  328. End Sub
  329.  
  330.  
  331. Private Sub PhiText_Change()
  332.     If ShowingParameters Then Exit Sub
  333.     EyePhi = CSng(PhiText.Text)
  334.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  335.     DrawData Pict
  336. End Sub
  337.  
  338.  
  339. Private Sub RText_Change()
  340.     If ShowingParameters Then Exit Sub
  341.     EyeR = CSng(RText.Text)
  342.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  343.     DrawData Pict
  344. End Sub
  345.  
  346.  
  347. Private Sub ThetaText_Change()
  348.     If ShowingParameters Then Exit Sub
  349.     EyeTheta = CSng(ThetaText.Text)
  350.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  351.     DrawData Pict
  352. End Sub
  353.  
  354.  
  355.  
  356.  
  357.  
  358.